/*  This file is part of Real Time Digital Audio Processing library.
 *  Copyright (C) 2008 - Emilio Monti - emilmont@gmail.com
 *
 *  Real Time Digital Audio Processing library is free software: you can
 *  redistribute it and/or modify it under the terms of the GNU General
 *  Public License as published by the Free Software Foundation, either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  Real Time Digital Audio Processing library is distributed in the hope
 *  that it will be useful, but WITHOUT ANY WARRANTY; without even the
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Real Time Audio Processing library.
 *  If not, see <http://www.gnu.org/licenses/>.
 */
#include <string.h>

#include "DSP.h"

DSP::DSP() {
    printf("[DSP]\n");
    sampleRate = 44100;
    framesPerBuffer = 205;

    mic_in = false;
}

void DSP::add(Module* module) {
    printf("   %s\n", module->descr);
    pipe.push_back(module);
}

void DSP::callback(float *inBuff, float *outBuff, unsigned long length) {
    /* Signal In */
    if (mic_in) {
        if (inBuff != NULL)
            memcpy(outBuff, inBuff, sizeof(float)*length);
    } else {
        memset(outBuff, 0, sizeof(float)*length);
    }

    for (unsigned int i = 0; i < pipe.size(); i++) {
        /* Process signal with the current pipeline module */
        pipe[i]->processSignal(outBuff, length);

        /* Check Overflow */
        if (pipe[i]->check_overflow == true) {
            for (unsigned int j=0; j<length; j++) {
                if (outBuff[i] > 1.0) {
                    printf("[OVERFLOW]%s\n", pipe[i]->descr);
                    outBuff[i] = 1.0;
                }
            }
        }
    }
}

DSP::~DSP() {
    for (unsigned int i = 0; i < pipe.size(); i++) {
        delete pipe[i];
    }
}
